<?php
namespace Admin\Controller;
use \Admin\Controller\IndexController;
class GoodsController extends IndexController 
{
    public function add()
    {
    	if(IS_POST)
    	{
                set_time_limit(0);
    		$model = D('Admin/Goods');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst?p='.I('get.p')));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
        //商品分类
        $catModel = D('Category');
        $catData=$catModel->getTree();
        $this->assign('catData',$catData); 
        //商品类型
        $typeModel = M('Type');
        $typeData=$typeModel->select();
        $this->assign('typeData',$typeData);  
        //商品品牌
        $brandModle = M('Brand');
        $brandData = $brandModle->select();
        $this->assign('brandData',$brandData);
        //会员信息
        $mlModel = M('MemberLevel');
        $mlData = $mlModel->select();
        $this->assign('mlData',$mlData);
        $this->setPageBtn('添加商品', '商品列表', U('lst?p='.I('get.p')));
        $this->display();
    }
    public function edit()
    {
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		$model = D('Admin/Goods');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
        //商品分类
        $catModel = D('Category');
        $catData=$catModel->getTree();
        $this->assign('catData',$catData); 
        //商品类型
        $typeModel = M('Type');
        $typeData=$typeModel->select();
        $this->assign('typeData',$typeData);  
        //商品品牌
        $brandModle = M('Brand');
        $brandData = $brandModle->select();
        $this->assign('brandData',$brandData);
        //会员信息
        $mlModel = M('MemberLevel');
        $mlData = $mlModel->select();
        $this->assign('mlData',$mlData);
        
        //取出要修改的商品信息
    	$model = M('Goods');
    	$data = $model->find($id);
    	$this->assign('data', $data);
        //取出当前商品的分类数据
        $gcModel = M('GoodsCat');
        $extCatId = $gcModel->field('cat_id')->where(array('goods_id'=>array('eq',$id)))->select();
        $this->assign('extCatId', $extCatId);
        
        //取出当前商品会员价格的数据
        $mpModel = M('MemberPrice');
        $_mpData = $mpModel->where(array('goods_id'=>array('eq',$id)))->select();
        //把二维数组变成一位数组
        $mpData = array();
        foreach ($_mpData as $k => $v) {
            $mpData[$v['level_id']]=$v['price'];
        }
        $this->assign('mpData',$mpData);
        //取出当前商品的属性数据
        $gaModel = M('GoodsAttr');
        $gaData = $gaModel->field('a.*,b.attr_name,b.attr_type,b.attr_option_values')
                ->alias('a')->join('LEFT JOIN sp_attribute b on a.attr_id=b.id')
                ->where(array('a.goods_id'=>array('eq', $id)))->order('a.attr_id ASC')->select();
        //取出当前商品属性不存在的后添加的新的属性
        //
        //取出当前商品已经有的属性id
        $attr_id=array();
        foreach ($gaData as $k => $v) {
            $attr_id[]=$v['attr_id'];
        }
        $attr_id= array_unique($attr_id);//去除数组重复
        if(!empty($attr_id)){
        //取出新属性
        $attrModel = M('Attribute');
        $otherAttr = $attrModel->field('id attr_id,attr_name,attr_type,attr_option_values')
                ->where(array('type_id'=>array('eq', $data['type_id']), 'id'=>array('not in', $attr_id)))->select();
        
        if($otherAttr){
            $gaData= array_merge($gaData,$otherAttr);
            usort($gaData, 'attr_id_sort');
        }}
        $this->assign('gaData', $gaData);
        //取出当前商品的图片
        $gpModel = M('GoodsPics');
        $gpData = $gpModel->where(array('goods_id'=>array('eq',$id)))->select();
        $this->assign('gpData',$gpData);
        
		$this->setPageBtn('修改商品', '商品列表', U('lst?p='.I('get.p')));
		$this->display();
    }
    public function recycle(){
        $model=M('Goods');
        $model->where(array(
            'id'=>array('eq',I('get.id')),
        ))->setField('is_delete',1);
        $this->success('操作成功!',U('lst',array('p'=>I('get.p',1))));
    }
    public function restore(){
        $model=M('Goods');
        $model->where(array(
            'id'=>array('eq',I('get.id')),
        ))->setField('is_delete',0);
        $this->success('操作成功!',U('recyclelst',array('p'=>I('get.p',1))));
    }
    public function delete()
    {
    	$model = D('Admin/Goods');
    	if($model->delete(I('get.id', 0)) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst', array('p' => I('get.p', 1))));
    		exit;
    	}
    	else 
    	{
    		$this->error($model->getError());
    	}
    }
    public function recyclelst(){
        $model = D('Admin/Goods');
    	$data = $model->search(20,1);
    	$this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
    	));

        $this->setPageBtn('商品回收站', '商品列表', U('lst'));
    	$this->display();
    }
    public function goodsNumber(){
        $goodsId=I('get.id');
        if(IS_POST){
            //添加之前先删除原来的数据
            $gnModel = M('GoodsNumber');
            $gnModel->where(array('goods_id'=>array('eq', $goodsId)))->delete();
            
            $gai=I('post.goods_attr_id');
            $gn=I('post.goods_number');
            $rate = count($gai) / count($gn);
            $_i = 0;
            foreach ($gn as $k => $v) {
                $_arr = array();
                for($i=0;$i<$rate;$i++){
                    $_arr[]=$gai[$_i];
                    $_i++;
                }
                sort($_arr);
                $_arr=  implode(',', $_arr);
                $gnModel->add(array(
                    'goods_id'=>$goodsId,
                    'goods_number'=>$v,
                    'goods_attr_id'=>$_arr,
                ));
            }
        }
        //根据商品id 取出多个属性值的属性
        $sql = 'select a.*,b.attr_name from sp_goods_attr a left join sp_attribute b on a.attr_id=b.id'
                . ' where attr_id in(select attr_id from sp_goods_attr where goods_id='.$goodsId
                .' group by attr_id having count(*)>1) and goods_id='.$goodsId;
        $db = M();
        $_attr = $db->query($sql);
        //处理数组 把属性相同的放在一起 二维转成三维
        $attr=array();
        foreach ($_attr as $k => $v) {
           $attr[$v['attr_id']][]=$v;
        }
        
        //取出当前商品已经设置的库存信息
        $gnModel=M('GoodsNumber');
        $gnData=$gnModel->where(array('goods_id'=>array('eq',$goodsId)))->select();
        $this->assign('gnData',$gnData);
        $this->assign('attr',$attr);
        $this->setpageBtn('商品库存', '商品列表', U('lst?p='.I('get.p')));
        $this->display();
    }

    public function lst()
    {
    	$model = D('Admin/Goods');
    	$data = $model->search();
    	$this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
    	));

        $this->setPageBtn('商品列表', '添加商品', U('add'));
    	$this->display();
    }
    //ajax取出分类属性
    public function ajaxGetAttr(){
        $typeId = I('get.type_id');
        $attrModel = M('Attribute');
        $attrData = $attrModel->where(array('type_id'=>array('eq',$typeId)))->select();
        echo json_encode($attrData);
    }
    public function ajaxDelImage() {
        $picId= I('get.pic_id');
        $gpModel=M('GoodsPics');
        //先从硬盘上删除图片
        $pic = $gpModel->field('pic,sm_pic')->find($picId);
        deleteImage($pic);
        //数据库上删除
        $gpModel->delete($picId);
    }
    public function ajaxDelGoodsAttr() {
        $gaId= I('get.gaid');
        $gaModel=M('GoodsAttr');
        $gaModel->delete($gaId);
    }
}